package de.tayrracer;

import java.awt.*;
import java.awt.image.BufferedImage;

public class TayrracerMain extends Canvas {
	private static final long serialVersionUID = 1L;
	private Image thePicture;
	PictureDrawer pd = null;
	
	public TayrracerMain(){
		startTracingTheRays();
	}

	public Dimension getPreferredSize() {
        return new Dimension(800,600);
    }
	
	private void startTracingTheRays(){
		thePicture = new BufferedImage(800, 600, BufferedImage.TYPE_INT_RGB);
		Graphics g = thePicture.getGraphics();
		pd = new PictureDrawer(g);
		Refresher refresher = new Refresher();
		refresher.start();
		pd.raytrace();
	}

    public void paint(Graphics g) {
        g.drawImage(thePicture, 0, 0, getBackground(), getParent());
    }
    
    private class Refresher extends Thread {

		@Override
		public void run(){
			while(pd.completeCount<pd.threadCount){
				TayrracerMain.this.repaint();
				try {
					Thread.sleep(100);
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			TayrracerMain.this.repaint();
		}
    }
}
